package com.tensquare.controller;

import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.pojo.Label;
import com.tensquare.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName BaseController
 * @Description: TODO
 * @Author zhangzheng
 * @Date 2019/12/16 0016
 **/
@RestController
@RequestMapping("/label")
public class BaseController {

    @Autowired
    private BaseService baseService;

    @PostMapping
    public Result addLable(@RequestBody Label label){
        try {
            baseService.addLable(label);
            return new Result(true,20000,"插入Label成功",null);
        }catch (Exception e) {
            return new Result(false, 20001, "插入Label成功", null);
        }
    }

    @GetMapping
    public Result getLabelList(){
        try {
            List<Label> labels = baseService.getLabelList();
            return new Result(true,20000,"查询成功",labels);
        }catch (Exception e) {
            return new Result(false, 20001, "查询失败", null);
        }

    }

    @GetMapping("{labelId}")
    public Result getLabelById(@PathVariable String labelId){
        try {
            Label label = baseService.getLabelById(labelId);
            return new Result(true,20000,"查询成功",label);
        }catch (Exception e) {
            return new Result(false, 20001, "查询失败", null);
        }

    }

    @PutMapping("{labelId}")
    public Result UpdateByLabel(@PathVariable String labelId,@RequestBody Label label){
        try {
            baseService.UpdateByLabel(labelId,label);
            return new Result(true,20000,"修改成功",null);
        }catch (Exception e) {
            return new Result(false, 20001, "修改失败", null);
        }
    }

    @DeleteMapping("{labelId}")
    public Result DeleteLabelById(@PathVariable String labelId){
        try {
            baseService.deleteLabelById(labelId);
            return new Result(true,20000,"删除成功",null);
        }catch (Exception e) {
            return new Result(false, 20001, "删除失败", null);
        }

    }

    @PostMapping("/Search/{page}/{size}")
    public Result SearchLabel(@PathVariable Integer page, @PathVariable Integer size){
        try {
            PageResult<Label> pageResult = baseService.search(page,size);
            return new Result(true,20000,"分页查询成功",pageResult);
        }catch (Exception e) {
            return new Result(false, 20001, "分页查询失败", null);
        }
    }
}
